using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XAEnima.Core.GameManager;
using XAEnima.Core.Stimulation.Tipi;
using XAEnima.Core.Stimulation.External;


namespace XAEnima.Core.Screens.Test
{


    public class Testtest : Microsoft.Xna.Framework.DrawableGameComponent
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        bool OldState = false;
        Texture2D UpSq;
        float screenscale;
        Rectangle area;
        Matrix SpriteScale;
        IInputManager InputManagerService;
        SpriteFont font;
        LEDDriver led;

        public Testtest(Game game)
            : base(game)
        {
            graphics = StateManager.graphic;
            InputManagerService = (IInputManager)Game.Services.GetService(typeof(IInputManager));
            InputManagerService.SPressed += new PressedEventHandler(SIsPressed);
            InputManagerService.SReleased += new PressedEventHandler(SIsReleased);
           /* InputManagerService.EnterPressed += new PressedEventHandler(EnterIsPressed);
            InputManagerService.EnterReleased += new PressedEventHandler(EnterIsReleased);
            InputManagerService.TabPressed += new PressedEventHandler(TabIsPressed);
            InputManagerService.UpPressed += new PressedEventHandler(UpIsPressed);
            InputManagerService.DownPressed += new PressedEventHandler(DownIsPressed);*/
            led = new LEDDriver();
        }


        public override void Initialize()
        {

            screenscale = (float)(Game.GraphicsDevice.Viewport.Width / 1600f);
            spriteBatch = (Game.Services.GetService(typeof(SharedContext)) as SharedContext).sprite_batch;
            area = Game.GraphicsDevice.Viewport.TitleSafeArea;
            Game.IsFixedTimeStep = true;
            Game.TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60);
            font = Game.Content.Load<SpriteFont>(@"Arial");

            base.Initialize();
        }


        protected override void LoadContent()
        {            
            UpSq = Game.Content.Load<Texture2D>(@"Stimulation/Chess1");
 
            float screenscale = (float)graphics.GraphicsDevice.Viewport.Width / 800f;
            SpriteScale = Matrix.CreateScale(screenscale, screenscale, 1);
        }


        public override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Game.Exit();

            

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            graphics.GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            SpriteFont c = Game.Content.Load<SpriteFont>("Arial");
            if (OldState == false)
            {
                //mySSVEP.DrawText(spriteBatch, "Arial", "Premere S per attivare stimolatori", new Vector2(300, 200));
                spriteBatch.DrawString(font, "Premere S per attivare stimolatori",
                   new Vector2(300, 200),
                   Color.Yellow,
                   0.0f, 
                   new Vector2(0, 0),
                   screenscale * 1.2f,
                   SpriteEffects.None, 0);
            }
            else
            {
                //mySSVEP.Draw(gameTime, spriteBatch, screenscale);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void SIsPressed() {
            if (!OldState)
            {

                if (led.init("COM4")==0)
                {
                    led.SetFrequencies(32, 34, 36, 38);
                    led.start();
                }
                var Stimulator = new Square(Game);
                Stimulator.SetFrequencies(32, 34, 36, 38);
                Game.Components.Add(Stimulator);
                
            }
            else {
                (Game.Components.ElementAt(Game.Components.Count - 1) as GameComponent).Dispose();
            }
        }

        public void SIsReleased()
        {
            OldState = !OldState;
        }
    }
}
